package com.wangsong.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Service;

@Service
public class RedissonUtils {

	@Autowired
	private JedisConnectionFactory jedisConnectionFactory;

	
	public String makeId(String name){
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		String dateString = formatter.format(currentTime);
		
		RedisAtomicLong rAtomicLong = new RedisAtomicLong(name,jedisConnectionFactory);
		if(rAtomicLong.get()==0
				||!String.valueOf(rAtomicLong.get()).substring(0, 8).equals(dateString)){
			rAtomicLong.set(Long.valueOf(dateString+"000000001"));
			return name+dateString+"000000001";
		}
		return name+String.valueOf(rAtomicLong.incrementAndGet());
	}
}